<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Student;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     * 
     */
    public function index()
    {
        $openid = $this->request->post('openid');
        $student = model('Student')->packageId($openid);
        // banner 数据
        $banner = Db::name('banner')->select();
        // 资讯板块数据
        $datas = Db::name('datas')->select();
        // 首页视频数据
        $video = Db::name('video')
//            ->where(function ($query) use($student){
//                if($student['subject']){
//                    $query->where('jy_video.subject_id','in',$student['subject'])
//                        ->where('jy_video.package_id>1 and jy_video.package_id <='.$student['package_id']);
//                }else{
//                    $query->where('jy_video.package_id',$student['package_id']);
//                }
//            })
            ->where('package_id',1)
            ->select();
        $videos = Db::name('video')
            ->where(function ($query) use($student){
                if($student['subject']){
                    $query->where('jy_video.subject_id','in',$student['subject'])
                        ->where('jy_video.package_id>1 and jy_video.package_id <='.$student['package_id']);
                }else{
                    $query->where('jy_video.package_id',$student['package_id']);
                }
            })
            ->select();
        $countTime = 0;
        foreach ($videos as $k=>$v){
            $length = model('Student')->getVideoInfo('http://yinglan.cdylsr.com'.$v['videofile']);
            $countTime += (int)$length['min'] * 60 + (int)$length['seconds'];
        }
        foreach ($video as $k=>$v){
            $length = model('Student')->getVideoInfo('http://yinglan.cdylsr.com'.$v['videofile']);
            $video[$k]['min'] = $length['min'];
            $video[$k]['sec'] = (int)$length['seconds'];
            $count = Db::name('history')->where("video_id = {$v['id']} and student_id = {$student['id']}")->value('count');
            $video[$k]['myMin'] = floor($count/60);
            $video[$k]['mySec'] = $count%60;
        }
        $myTime = Db::name('history')->where('student_id',$student['id'])->sum('count');
        return json_encode(array('banner'=>$banner,'datas'=>$datas,'video'=>$video,'countTime'=>$countTime,'myTime'=>$myTime));
    }
    // 咨询详情
    public function about(){
        $id = $this->request->get('id');
        $typeId = $this->request->get('typeId');
        $table = $typeId == 10?'banner':'datas';
        if($id > 0){
            $res = Db::name($table)->where('id',$id)->find();
        }else{
            $res = Db::name($table)->where('id',$typeId)->find();
        }
        return json_encode($res);
    }
    //用户信息
    public function info(){
        $openid = $this->request->get('openid');
        $info = Db::name('student')
            ->alias('a')
            ->join('jy_package p','a.package_id = p.id')
            ->where('openid',$openid)
            ->field(['a.id as ids,a.name as names,a.image,p.name'])
            ->select();
        $data = array();
        $data['id'] = $info[0]['ids'];
        $data['member'] = $info[0]['names'];
        $data['type'] = $info[0]['name'];
        $data['avatar'] = $info[0]['image'];
        return json_encode($data);
    }
    // 用户ID
    public function studentId(){
        $openid = $this->request->get('openid');
        $student = model('Student')->packageId($openid);
        return json_encode($student['id']);
    }
    // 用户及上家信息
    public function parent(){
        $info = Db::name('student')->where('openid',$this->request->get('openid'))->find();
        if($info['student_id'] > 0){
            // 代表有上家
            $parent = Db::name('student')->where('id',$info['student_id'])->find();
            $res = Db::name('student')->where('student_id',$parent['id'])->limit(2)->select();
            $res[] = $parent;
        }else{
            $res = Db::name('student')->where('student_id',$info['id'])->limit(2)->select();
            $res[] = $info;
        }
        if(count($res) >= 3){
            foreach ($res as $k=>$v){
                $count = Db::name('couponorder')->where('student_id',$v['id'])->count();
                if($count < 1){
                    $arr = array(
                        'student_id'    => $v['id'],
                        'coupon_id'     => 1,
                        'createtime'    => time()
                    );
                    Db::name('couponorder')->insert($arr);
                }
            }
        }
        return json_encode($res);
    }
    //  我的优惠券
    public function myCoupon(){
        $openid = $this->request->get('openid');
        $student = model('Student')->packageId($openid);
        $res = Db::name('couponorder')->where('student_id',$student['id'])->find();
        if($res){
            $image = Db::name('coupon')->where('id',$res['coupon_id'])->value('image');
        }else{
            $image = 0;
        }
        return json_encode($image);
    }
    // 老师信息
    public function teacher(){
        $teacherName = Db::name('config')->where('name','teacherName')->value('value');
        $teacherTou = Db::name('config')->where('name','teacherTou')->value('value');
        $teacherPhone = Db::name('config')->where('name','teacherPhone')->value('value');
        $teacherWx = Db::name('config')->where('name','teacherWx')->value('value');
        $teacherImg = Db::name('config')->where('name','teacherImg')->value('value');
        return json_encode(array('teacherName'=>$teacherName,'teacherTou'=>$teacherTou,'teacherPhone'=>$teacherPhone,'teacherWx'=>$teacherWx,'teacherImg'=>$teacherImg));
    }
    // 报名查分
    public function remarks(){
        $res = Db::name('config')->where('id','>',22)->select();
        return json_encode($res);
    }
}
